<?php
namespace isoftstone\ueditor;

use Flarum\Http\Controller\ControllerInterface;
use Flarum\Settings\SettingsRepositoryInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Flarum\Foundation\Application;
use isoftstone\ueditor\FileUploader;

class UeditorController implements ControllerInterface
{

    /**
     * @var SettingsRepositoryInterface
     */
    protected $settings;
    protected $app;

    /**
     * TestController constructor.
     * @param SettingsRepositoryInterface $settings
     */
    public function __construct(SettingsRepositoryInterface $settings, Application $app)
    {
        $this->settings = $settings;
        $this->app = $app;
    }

    /**
     * @param Request $request
     * @return string
     */
    public function handle(Request $request)
    {
        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(dirname(__FILE__) . "/config.json")), true);
        $action = $_GET['action'];

        switch ($action) {
            case 'baseurl':
                $result = 'var __ueditorBaseUrl=' . $this->app->url();
                break;
            case 'config':
                      $CONFIG['imageUrlPrefix']
                    = $CONFIG['scrawlUrlPrefix']
                    = $CONFIG['snapscreenUrlPrefix']
                    = $CONFIG['catcherUrlPrefix']
                    = $CONFIG['videoUrlPrefix']
                    = $CONFIG['fileUrlPrefix']
                    = $CONFIG['imageManagerUrlPrefix']
                    = $CONFIG['fileManagerUrlPrefix']
                    = $this->app->url();
                $result =  json_encode($CONFIG);
                break;
            /* 上传图片 */
            case 'uploadimage':
                /* 上传涂鸦 */
            case 'uploadscrawl':
                /* 上传视频 */
            case 'uploadvideo':
                /* 上传文件 */
            case 'uploadfile':
                $result = include(dirname(__FILE__) . "/action_upload.php");
                break;
            /* 列出图片 */
            case 'listimage':
                $result = include(dirname(__FILE__) . "/action_list.php");
                break;
            /* 列出文件 */
            case 'listfile':
                $result = include(dirname(__FILE__) . "/action_list.php");
                break;
            /* 抓取远程文件 */
            case 'catchimage':
                $result = include(dirname(__FILE__) . "/action_crawler.php");
                break;
            default:
                $result = json_encode(array(
                    'state'=> '请求地址出错'
                ));
                break;
        }

        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }
        exit;
    }

}
